﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading.Tasks;
using System.Data.Entity;
using ESupervision.Models;

namespace ESupervision.Services
{
    public class MessageService : IMessageService
    {

        public Task<int> SendMessage( ApplicationDbContext db, Message message, List<ApplicationUser> receivers)
        {
            db.Messages.Add(message);
            db.SaveChanges();
            // Create MesageRecipients
            foreach (ApplicationUser receiver in receivers)
            {
                MessageRecipient messageRecipient = new MessageRecipient
                {
                    Message = message,
                    ReceiverUserID = receiver.Id,
                    MessageStatus = MessageStatus.Unread,
                    ReceivedTime = message.SentTime
                };
                db.MessageRecipients.Add(messageRecipient);
            }

            MessageSent messageSent = new MessageSent
            {
                Message = message,
                SentUserID = message.SentByUserId,
                MessageStatus = MessageStatus.Read,
                SentTime = message.SentTime
            };


            db.MessageSents.Add(messageSent);

            return db.SaveChangesAsync();
        }


        public async Task DeleteReceivedMessage(ApplicationDbContext db, int id)
        {
            var messageRecipient = await db.MessageRecipients.FindAsync(id);

            if (messageRecipient != null)
            {
                db.MessageRecipients.Remove(messageRecipient);
                await db.SaveChangesAsync();
            }
        }


        public async Task DeleteSentMessage(ApplicationDbContext db, int id)
        {
            var messageSent = await db.MessageSents.FindAsync(id);
            if (messageSent != null)
            {
                db.MessageSents.Remove(messageSent);
                await db.SaveChangesAsync();
            }
        }




        public async Task<List<MessageRecipient>> GetInboxMessages(ApplicationDbContext db, string userId)
        {
            var inBoxMessages = from b in db.MessageRecipients
                                where b.ReceiverUserID == userId
                                orderby b.ReceivedTime descending
                                select b;
            inBoxMessages.Include(m => m.Message).Include(n => n.Message.Sender);
            return await inBoxMessages.ToListAsync();
        }




        public async Task<List<MessageSent>> GetSentMessages(ApplicationDbContext db, string userId)
        {
            var sentMessages = from b in db.MessageSents
                               where b.SentUserID == userId
                               orderby b.SentTime descending
                               select b;
            sentMessages.Include(m => m.Message).Include(m => m.Message.MessageRecipients).Include(m => m.Message.MessageRecipients.Select(n => n.ReceiverUser));

            return await sentMessages.ToListAsync();
        }
    }
}